using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

using phorm.database.core;
using phorm.model.fields;

namespace phorm.database.oracle
{
    public class OracleRecord : DatabaseRecord
    {
        public override string create()
        {
            return CRUD.buildTable(this);
        }

        public override string createManyToMany(BaseManyToMany column)
        {
            return CRUD.createManyToMany(column);
        }

        public override void save()
        {
            OracleDatabase.connection();
            bool hasChanged = false;
            foreach (KeyValuePair<string, DatabaseColumn> column in this.columns)
            {
                if (!(column.Value is PrimaryKey) && column.Value.hasChanged)
                    hasChanged = true;
            }

            if (this.IsNew)
                this.primary_key.value = OracleDatabase.idQuery(CRUD.buildSaveQuery(this));
            else if (hasChanged)
                OracleDatabase.query(CRUD.buildSaveQuery(this));
            this.IsNew = false;
            OracleDatabase.finish();
        }

        public override string empty()
        {
            return CRUD.buildEmptyQuery(this);
        }

        public override string drop()
        {
            return CRUD.buildDropQuery(this);
        }

    }
}
